package main

import (
	"fmt"
	"go-xorm-01/utils"
	"time"
	"xorm.io/xorm"
)

func main() {
	engine := utils.GenerateEngine()
	//自动同步结构体到数据库
	err := engine.Sync2(new(User))
	if err != nil {
		panic(err)
	}
	defer engine.Close()

	InsertUser(engine)
	//InsertUserList(engine)
	//InsertUserSlice(engine)
	//UpdateUser(engine)
	//DeleteUser(engine)
	//SelectUser(engine)
	//SelectUserList(engine)

	//Transaction(engine)
}

type User struct {
	Id            int64     `xorm:"id"`
	Username      string    `xorm:"username"`
	Password      string    `xorm:"password"`
	RegisterTime  time.Time `xorm:"register_time"`
	LastLoginTime time.Time `xorm:"last_login_time"`
}

func InsertUser(engine *xorm.Engine) {
	insert, err := engine.Insert(&User{
		Username:      "tristan",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	})
	if err != nil {
		return
	}
	fmt.Println(insert)
}

func InsertUserList(engine *xorm.Engine) {
	user1 := User{
		Username:      "tristan",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	}

	user2 := User{
		Username:      "tristan",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	}
	insert, err := engine.Insert(&user1, &user2)
	if err != nil {
		return
	}
	fmt.Println(insert)
}

func InsertUserSlice(engine *xorm.Engine) {
	var userList []User = make([]User, 0)
	user1 := User{
		Username:      "zhangsan",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	}
	user2 := User{
		Username:      "lisi",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	}
	userList = append(userList, user1, user2)
	insert, err := engine.Insert(&userList)
	if err != nil {
		return
	}
	fmt.Println(insert)
}

func UpdateUser(engine *xorm.Engine) {
	user := User{
		Username: "tristan--up",
	}
	update, err := engine.Where("id = ?", 1).Update(&user)
	if err != nil {
		panic(err)
	}
	fmt.Println(update)

}

func DeleteUser(engine *xorm.Engine) {
	delete, err := engine.Where("id = ?", 1).Delete(&User{})
	if err != nil {
		panic(err)
	}
	fmt.Println(delete)
}

func SelectUser(engine *xorm.Engine) {
	res1, err := engine.Query("select * from user")
	fmt.Println(res1)

	fmt.Println()
	res2, err := engine.QueryInterface("select * from user")
	fmt.Println(res2)

	fmt.Println()
	res3, err := engine.QueryString("select * from user")
	fmt.Println(res3)

	fmt.Println()
	var user User
	engine.Where("id = ?", 2).Get(&user)
	fmt.Println(user)

	fmt.Println()
	var name string
	engine.Table("user").Where("id = ?", 2).Cols("username").Get(&name)
	fmt.Println(name)

	fmt.Println(err)

}

func SelectUserList(engine *xorm.Engine) {
	var userList []User
	engine.Where("id > ?", 2).Limit(10, 0).Find(&userList)
	fmt.Println(userList)
	fmt.Println()

	var user User
	count, _ := engine.Where("id > ?", 2).Count(&user)
	fmt.Println(count)
	fmt.Println()

	engine.Iterate(&User{Password: "123456"}, func(idx int, bean interface{}) error {
		user := bean.(*User)
		fmt.Println(user)
		return nil
	})
	fmt.Println()

	rows, _ := engine.Rows(&User{Password: "123456"})
	for rows.Next() {
		var user User
		rows.Scan(&user)
		fmt.Println(user)
	}

}

func Transaction(engine *xorm.Engine) {

	session := engine.NewSession()
	defer session.Close()

	//开启事务
	session.Begin()

	defer func() {
		if p := recover(); p != nil {
			//回滚
			fmt.Println("panic---", p)
			session.Rollback()
		} else {
			//提交
			session.Commit()
		}
	}()

	session.Insert(User{
		Id:            1,
		Username:      "AAAAA",
		Password:      "123456",
		RegisterTime:  time.Now(),
		LastLoginTime: time.Now(),
	})

	session.Where("id=?", 7).Delete(&User{})

}
